package cn.com.pxto.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

import cn.com.pxto.dao.DicDAO;
import cn.com.pxto.model.Dic;

public class DicValueTag extends BodyTagSupport {
	
	private String type;
	private String value;
	private DicDAO dicDAO;
	private static Log log=LogFactory.getLog(DicValueTag.class);
	//dic����
	public void setType(String type) {
		this.type = type;
	}
	//dicֵ
	public void setValue(String value) {
		this.value = value;
	}
	
	public DicValueTag(){
		
	}
	
	public int doStartTag() throws JspException {
		//��ϵͳ�л�ȡspring����
		ApplicationContext ctx = (XmlWebApplicationContext)pageContext.getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
		dicDAO = (DicDAO)ctx.getBean("dicDAO");
				//���ò��Ҳ���
		int value = -1;
		try{
			value = Integer.parseInt(this.value);
		}
		catch(NumberFormatException e){
			//throw new JspException("dicValue:generate an error when coverate string to int!");
		}
		//���ҽ��
		Dic dic = dicDAO.getDicByTypeAndID(this.type,new Integer(value));
		//�����
		try {
			if (dic==null){
				pageContext.getOut().print("Unknown");
			}else{
				pageContext.getOut().print(dic.getName());
			}
		} catch (IOException e) {
			throw new JspException("Print Error:" + 
                    e.getMessage(  ));
		}
		return super.doStartTag();
	}
	
	
	
}
